define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'allocation/index' + location.search,
                    add_url: 'allocation/add',
                    // edit_url: 'allocation/edit',
                    // del_url: 'allocation/del',
                    multi_url: 'allocation/multi',
                    import_url: 'allocation/import',
                    table: 'allocation',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                showSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type_text', title: __('Type'),searchList:false},
                        {field: 'to_type_text', title: __('To_type')},
                        {field: 'to_text', title: __('To')},
                        {field: 'product_id', title: __('产品')},
                        {field: 'number', title: __('Number')},
                        {field: 'confirm_type_text', title: __('Confirm_type')},
                        {field: 'order_id', title: "来源",formatter:function (value) {
                                return value > 0 ? "<p style='color: #02f8c6;'>订单</p>" : "默认" ;
                            }},
                        {field: 'confirm_time', title: __('Confirm_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'check',
                                text: __('接收'),
                                icon: 'fa fa-check-square',
                                classname: 'btn btn-warning btn-xs btn-detail btn-dialog',
                                url: 'allocation/check',
                                hidden:function (row) {
                                    return (row.confirm_type == 0 && row.to == Config.company_id) || (row.confirm_type == 0 && row.type == 2 && row.to == Config.company_id) ? false : true;
                                }
                            },{
                                name: 'detail',
                                text: __('详情'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'allocation/detail'
                            }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on('change', "#c-type", function () {
                const val = $(this).val();
                if(val == 1){
                    $(".stock").show();
                }else if(val == 2){
                    $(".stock").hide();
                }
            });

            $(document).on('change', "#c-to_type", function () {
                const val = $(this).val();
                if(val == 1){
                    $("#company").show();
                    $("#user").hide();
                }else if(val == 2){
                    $("#company").hide();
                    $("#user").show();
                }
            });

            $(document).on('change', "#c-data_type", function () {
                const val = $(this).val();
                if(val == 1){
                    $("#file_url").show();
                    $("#sn").hide();
                }else if(val == 2){
                    $("#file_url").hide();
                    $("#sn").show();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        check: function () {
            Controller.api.bindevent();
            $(document).on('change', "input[name=confirm_type]", function () {
                const val = $(this).val();
                if(val == 1){
                    $("#remark").hide();
                }else if(val == 2){
                    $("#remark").show();
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
